import { Container, Inject } from '@rockerjs/core';

class User {
    id: string = "testId";
    name: string = "testName";
}

class UserService {
    getUser(_id: string): User {
        return new User();
    }
}

@Inject
class ControlDefault {
    @Inject
    userService: UserService;

    test() {
        let user: User = this.userService.getUser("test");
        console.log(user);
    }
}

@Inject('controllor-with-args', new Date())
class ControlDefaultWithArgs {
    name: string;
    time: Date;

    constructor(name: string, time: Date) {
        this.name = name;
        this.time = time;
    }

    @Inject
    userService: UserService;

    test() {
        let user: User = this.userService.getUser("test");
        console.log(user, this.name, this.time);
    }
}

@Inject('controllor1', 'util', new Date())
class Control {
    name: string;
    time: Date;

    constructor(name: string, time: Date) {
        this.name = name;
        this.time = time;
    }

    @Inject
    userService: UserService;

    test() {
        let user: User = this.userService.getUser("test");
        console.log(user, this.name, this.time);
    }
}

// 通过getObject接口从容器中获取实例，参数为“单例的名称”（默认名称为类名首字母小写）
Container.getObject<ControlDefault>('controlDefault').test();
// 通过getObject接口从容器中获取实例，此例中并未提供实例名
Container.getObject<ControlDefaultWithArgs>('controlDefaultWithArgs').test();
// 通过getObject接口从容器中获取实例，此例中提供了3个参数，@rockerjs/core 认为第一个参数为实例名，剩下的参数则用于实例化
Container.getObject<Control>('controllor1').test();
